<template>
  <elm-page-bb class="ei_edit">
    <el-form ref="model" :model="model" size="small" label-width="120px">
    <elm-row>
      <el-form-item label="工号" prop="emp_code" :rules="[{ required: true, message: '工号必须填写'}]">
        <el-input v-model="model.emp_code"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name" :rules="[{ required: true, message: '姓名必须填写'}]">
        <el-input v-model="model.name"></el-input>
      </el-form-item>
    </elm-row>
      <el-form-item label="所在部门">
        <elm-tag :items="orgs" style="margin-left: -3px;" width="200px">
          <template  #text="{ item }">
            <span style="margin-right: 5px;">{{item.name}}</span>
            <span style="margin-right: 5px;">|</span>
            <el-checkbox v-model="item.fzr" :true-label="1" :false-label="0" size="mini" style="margin-right: 5px;">负责人</el-checkbox>
            <span style="margin-right: 5px;">|</span>
            <el-checkbox v-model="item.pm" :true-label="1" :false-label="0" size="mini" @change="pm_change(item)">主部门</el-checkbox>
          </template >
          <elm-org  @changed="orgSelect"  v-model="org" clearable></elm-org>
        </elm-tag>
      </el-form-item>
      <elm-row>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="model.sex"  style="width: 100%">
          <el-option key="男" label="男" value="男"></el-option>
          <el-option key="女" label="女" value="女"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="民族" prop="nation">
        <el-select v-model="model.nation" style="width: 100%">
          <el-option v-for="nation in nations" :key="nation" :label="nation" :value="nation"></el-option>
        </el-select>
      </el-form-item>
    </elm-row>
      <elm-row>
        <el-form-item label="身份证号" prop="id_card">
          <el-input v-model="model.id_card"></el-input>
        </el-form-item>
        <el-form-item label="出生日期" prop="birthday">
          <el-date-picker v-model="model.birthday"  value-format="yyyy-MM-dd" type="date" style="width: 100%">
          </el-date-picker>
        </el-form-item>
      </elm-row>
      <el-form-item label="联系电话" prop="phone">
        <elm-tag :items="phones"  style="margin-left: -3px;" width="150px">
          <template  #text="{ item }">
            {{item}}
          </template >
          <el-input v-model="phone" @change="addphone"></el-input>
        </elm-tag>
      </el-form-item>
      <el-form-item label="家庭住址" prop="home_address">
        <el-input  type="textarea" :rows="2" v-model="model.home_address"></el-input>
      </el-form-item>
      <elm-row>
        <el-form-item label="婚姻状况" prop="marital_status">
          <el-select v-model="model.marital_status"  style="width: 100%">
            <el-option key="未婚" label="未婚" value="未婚"></el-option>
            <el-option key="已婚" label="已婚" value="已婚"></el-option>
            <el-option key="离异" label="离异" value="离异"></el-option>
            <el-option key="丧偶" label="丧偶" value="丧偶"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="政治面貌" prop="politics_status">
          <el-select v-model="model.politics_status"  style="width: 100%">
            <el-option key="中共党员" label="中共党员" value="中共党员"></el-option>
            <el-option key="群众" label="群众" value="群众"></el-option>
            <el-option key="中共预备党员" label="中共预备党员" value="中共预备党员"></el-option>
            <el-option key="共青团员" label="共青团员" value="共青团员"></el-option>
            <el-option key="民革党员" label="民革党员" value="民革党员"></el-option>
            <el-option key="民盟盟员" label="民盟盟员" value="民盟盟员"></el-option>
            <el-option key="民建会员" label="民建会员" value="民建会员"></el-option>
            <el-option key="民进会员" label="民进会员" value="民进会员"></el-option>
            <el-option key="农工党党员" label="农工党党员" value="农工党党员"></el-option>
            <el-option key="致公党党员" label="致公党党员" value="致公党党员"></el-option>
            <el-option key="九三学社社员" label="九三学社社员" value="九三学社社员"></el-option>
            <el-option key="台盟盟员" label="台盟盟员" value="台盟盟员"></el-option>
            <el-option key="无党派人士" label="无党派人士" value="无党派人士"></el-option>
          </el-select>
        </el-form-item>
      </elm-row>
      <elm-row>
        <el-form-item label="用工状态" prop="work_status">
          <el-select v-model="model.work_status"  style="width: 100%">
            <el-option key="全职" label="全职" value="全职"></el-option>
            <el-option key="兼职" label="兼职" value="兼职"></el-option>
            <el-option key="离职" label="离职" value="离职"></el-option>
            <el-option key="实习" label="实习" value="实习"></el-option>
            <el-option key="临时工" label="临时工" value="临时工"></el-option>
            <el-option key="外包" label="外包" value="外包"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入职时间" prop="enter_company_date">
          <el-date-picker v-model="model.enter_company_date" value-format="yyyy-MM-dd" type="date" style="width: 100%">
          </el-date-picker>
        </el-form-item>
      </elm-row>
      <elm-row>
        <el-form-item label="参加工作时间" prop="work_date">
          <el-date-picker v-model="model.work_date" type="date"   value-format="yyyy-MM-dd" style="width: 100%">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="学历" prop="education">
          <el-select v-model="model.education"  style="width: 100%">
            <el-option key="本科" label="本科" value="本科"></el-option>
            <el-option key="研究生" label="研究生" value="研究生"></el-option>
            <el-option key="大专" label="大专" value="大专"></el-option>
            <el-option key="博士" label="博士" value="博士"></el-option>
            <el-option key="高中" label="高中" value="高中"></el-option>
            <el-option key="中专" label="中专" value="中专"></el-option>
            <el-option key="初中" label="初中" value="初中"></el-option>
            <el-option key="小学" label="小学" value="小学"></el-option>
          </el-select>
        </el-form-item>
      </elm-row>
      <el-form-item label="员工照片">
        <elm-upload module_name="biz_hrm_ei_basic_avatar" :rel_id="model.id" :multiple="false" :limit="1"  list-type="picture" tip=" "></elm-upload>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" :rows="2" v-model="model.remark"></el-input>
      </el-form-item>
    </el-form>

    <template #bottom>
      <el-button type="success" size="small" @click="submit">立即保存</el-button>
      <el-button size="small" @click="close">取消</el-button>
    </template>
  </elm-page-bb>
</template>

<script>
export default {
  extends: elmVueBase,
  data() {
    return {
      model: {phone: "", id: elm.newGuid()},
      orgs:[],
      org:null,
      phones:[],
      phone:null,
      nations: [
        '汉族', '蒙古族', '回族', '藏族', '维吾尔族', '苗族', '彝族', '壮族', '布依族',
        '朝鲜族', '满族', '侗族', '瑶族', '白族', '土家族', '哈尼族', '哈萨克族', '傣族',
        '黎族', '傈僳族', '佤族', '畲族', '高山族', '拉祜族', '水族', '东乡族', '纳西族',
        '景颇族', '柯尔克孜族', '土族', '达斡尔族', '仫佬族', '羌族', '布朗族', '撒拉族',
        '毛南族', '仡佬族', '锡伯族', '阿昌族', '普米族', '塔吉克族', '怒族', '乌孜别克族',
        '俄罗斯族', '鄂温克族', '德昂族', '保安族', '裕固族', '京族', '塔塔尔族', '独龙族',
        '鄂伦春族', '赫哲族', '门巴族', '珞巴族', '基诺族'
      ]
    }
  },
  async created() {
    this.phones=(this.model.phone==null || this.model.phone=='') ?[]:this.model.phone.split(",");
    var res = await elm.post('/business/my/ei/GetOrgs', {"ei_id": this.model.id });
    this.orgs=res;
  },
  methods: {
    pm_change(item)
    {
      for (var obj of this.orgs) {
        if(obj!=item)
        {
          obj.pm=0;
        }
      }
    },
    orgSelect(id,name) {
      if(!this.orgs.some(p=>p.id==id)) {
        this.orgs.push({id: id, name: name,fzr:0,pm:0});
        this.org = null;
      }
      else{
        this.error("部门已存在");
      }
    },
    addphone()
    {
      if(this.phones.indexOf(this.phone)==-1) {
        this.phones.push(this.phone);
        this.phone = null;
      }
      else{
        this.error("电话号码已存在");
      }
    },
    async submit() {
      await this.$refs["model"].validate();
      var res = await elm.post('/business/my/ei/Any', { "emp_code": this.model.emp_code, "id": this.model.id });
      if (res == true) {
        this.$message.error('' + this.model.emp_code + '号员工已存在!');
      }
      else {
        this.model.phone=this.phones.join(",");
        var res = await elm.post('/business/my/ei/Upsert', {mod:this.model,orgs:this.orgs});
        if (res == true) {
          this.v('保存成功。')
          this.close();
          if(this.request.p!=undefined && this.request.p=="ei_record")
          {
            this.request.callVue.init(this.model);
          }
          else
          {
            this.request.callVue.tablebinding();
          }
        }
        else {
          this.error('保存失败，请稍后尝试。');
        }
      }
    },
  }
}
</script>

<style>
.ei_edit .el-checkbox__label
{
   padding-left: 3px;
   font-size: 12px;
}
</style>